package com.factory.test.main;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;

import androidx.databinding.DataBindingUtil;

import com.factory.test.R;
import com.factory.test.base.BaseActivity;
import com.factory.test.base.MyApplication;
import com.factory.test.databinding.ActivityMainBinding;
import com.factory.test.util.DataSaveUtil;

public class MainActivity extends BaseActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // 避免从桌面启动程序后，会重新实例化入口类的activity
        if (!this.isTaskRoot()) {   //当前类不是该Task的根部，那么之前启动
            Intent intent = getIntent();
            if (intent != null) {
                String action = intent.getAction();
                if (intent.hasCategory(Intent.CATEGORY_LAUNCHER) && Intent.ACTION_MAIN.equals(action)) { // 当前类是从桌面启动的
                    finish(); // finish掉该类，直接打开该Task中现存的Activity
                    Log.i("start", "当前类是从桌面启动的, 直接打开该Task中现存的Activity");
                    return;
                }
            }
        }

        binding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        binding.versionName.setText("Version: " + MyApplication.getMyApplication().getAppVersionName());

        binding.test1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, TestMainActivity.class);
                intent.putExtra("testGroupType", 1);            //功能测试1
                intent.putExtra("subTitle", binding.test1.getText().toString());
                startActivity(intent);
            }
        });

        binding.test2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, TestMainActivity.class);
                intent.putExtra("testGroupType", 2);            //功能测试2
                intent.putExtra("subTitle", binding.test2.getText().toString());
                startActivity(intent);
            }
        });

        binding.test3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(context, TestMainActivity.class);
                intent.putExtra("testGroupType", 3);            //功能测试2
                intent.putExtra("subTitle", binding.test3.getText().toString());
                intent.putExtra("showAll", true);
                startActivity(intent);
            }
        });

        binding.test3bg.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                binding.test3bg.setVisibility(View.GONE);
                binding.test3.setVisibility(View.VISIBLE);
                return true;
            }
        });

        Intent intent = new Intent(context, TestMainActivity.class);
        intent.putExtra("testGroupType", 3);            //功能测试2
        intent.putExtra("subTitle", binding.test3.getText().toString());
        intent.putExtra("showAll", true);
        startActivity(intent);

        finish();
    }

    private MenuItem menuItem;


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_toggle_mode, menu);
        menuItem = menu.findItem(R.id.toggle_mode1);
        int index = DataSaveUtil.getInstance().getDesignIndex();
        if(index == 0){
            menuItem.setTitle("切换到横屏模式");
        }else{
            menuItem.setTitle("切换到竖模式");
        }
        return super.onCreateOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if(item.getItemId() == R.id.toggle_mode1){
            int index = DataSaveUtil.getInstance().getDesignIndex();
            if(index == 0){
                index = 1;
            }else{
                index = 0;
            }
            DataSaveUtil.getInstance().setDesignIndex(index);
            MyApplication.reStartAutoSize();
            finish();
            Intent intent = new Intent(context, MainActivity.class);
            startActivity(intent);
        }else if(item.getItemId() == R.id.launcher3){
            gotoLauncher3(context);
        }
        return super.onOptionsItemSelected(item);
    }

    public static void gotoLauncher3(Context context) {
        //startAPP("com.android.launcher3", "com.android.launcher3.Launcher");
        try {
            Intent intent = new Intent();
            ComponentName cn =  new ComponentName("com.android.launcher3", "com.android.launcher3.Launcher");
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {
                cn = new ComponentName("com.android.launcher3", "com.android.launcher3.uioverrides.QuickstepLauncher");
            }
            intent.setComponent(cn);
            //intent.addCategory(Intent.CATEGORY_LAUNCHER);
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {
            Log.e("launcher", "launcher3 启动失败", e);
        }
    }
}
